﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

/// <summary>
///MemDisplay 的摘要说明
/// </summary>
public class MemDisplay
{
    private static double MINPOSTSPAN = 8.0;

    public MemDisplay()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public MemDisplay(members m)
    {
        this.uid = m.uid;
        this.username = m.username;
        this.nickname = m.nickname;
        this.hasAvatar = m.hasavatar;
        this.gid = m.gid;
        this.onlineTime = m.onlinetime;
    }

    public MemDisplay(int uid)
    {
        GetData gd = new GetData();
        members m = gd.GetOneMember(uid);
        if (m != null)
            this.isExist = 1;
        else
        {
            this.isExist = 0;
            return;
        }
        this.uid = m.uid;
        this.gid = m.gid;
        this.username = m.username;
        this.nickname = m.nickname;
        this.hasAvatar = m.hasavatar;
        if (m.gender == 1)
            this.gender = "男";
        else if (m.gender == 0)
            this.gender = "女";
        else
            this.gender = "未知";

        if (m.QQ != null)
            this.QQ = m.QQ;
        else
            this.QQ = "";
        if (m.brithday != Nulltime)
        {
            this.birthday = ((DateTime)m.brithday).ToString("yyyy-MM-dd");
            birth = (DateTime)m.brithday;
        }
        else
            this.birthday = "- - -";
        if(m.email!=null)
            this.email = m.email;
        if (m.department != null)
            this.department = m.department;

        this.onlineTime = m.onlinetime;
        this.regTime = m.reg_time;
        this.regIP = m.reg_ip;
        
        this.topicSum = m.threadsnum;
        this.viewSum = m.viewnum;
        this.repSum = m.replynum;
        this.credit = m.credits;
        this.lastLoginTime = m.lastlogin_time;
        this.lastPostIP = m.lastlogin_ip;
        //this.password = m.password;

    }

    #region 变量声明

    public int uid;
    public string username;
    public int isExist = 0;
    public string nickname;
    public int hasAvatar;
    public int gid;
    public string gender;
    public string QQ;
    public string birthday;
    public string department;
    public DateTime birth;
    public DateTime? Nulltime = null;
    public string email;
    public int onlineTime;
    public TimeSpan online;
    public DateTime regTime;
    public string regIP;
    public int topicSum;
    public int viewSum;
    public int repSum;
    public int credit;
    public DateTime lastLoginTime;
    public string lastPostIP;
    public string password;
    #endregion

    public void SetBirth()//设置生日
    {
        birth = ConvertData.StrToDate(birthday);
    }

    public void UpdateAvatar(string originalPath,string mode)//上传头像
    {
        string desPath="";
        Image originalImage = Image.FromFile(originalPath);
        int ow = originalImage.Width;
        int oh = originalImage.Height;
        int toWidth = 0;
        int toHeight = 0;
        switch(mode)
        {
            case "Small":
                toHeight = toWidth = 50;
                desPath=System.IO.Path.GetDirectoryName(originalPath)+"\\"+this.uid.ToString()+"-small.png";
                break;
            case "Big":
                toWidth = 150;
                toHeight = (int)(toWidth*1.0 / ow * oh);
                desPath = System.IO.Path.GetDirectoryName(originalPath) + "\\" + this.uid.ToString() + "-big.png";
                break;
            default:
                break;
        }

        Image bitmap = new System.Drawing.Bitmap(toWidth, toHeight);//新建一个bmp图片
        Graphics g = System.Drawing.Graphics.FromImage(bitmap);//新建一个画板
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度
        g.Clear(Color.White);//清空画布并以透明背景色填充
        g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(0, 0, ow, oh), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分
        try
        {
            if (System.IO.File.Exists(desPath))
                System.IO.File.Delete(desPath);
            bitmap.Save(desPath,System.Drawing.Imaging.ImageFormat.Png);
            
        }
        catch (System.Exception ex)
        {
        	throw ex;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }

    }

    public void Update()//更新用户信息
    {
        try
        {
            SaveData.UpdateMember(this);
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public void Regist()//注册用户
    {
        try
        {
            //SaveData.Regist(this);
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public void DeRegist()//删除用户
    {
        try
        {
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public static void ViewTopic(int uid)//用户浏览帖子
    {
        try
        {
            SaveData.IncMemViewNum(uid);
        }
        catch (System.Exception ex)
        {
        	//出错处理
        }
    }

    public static void PostTopic(int uid)//用户发帖
    {
        try
        {
            SaveData.IncMemTopNum(uid,DateTime.Now);
        }
        catch (System.Exception ex)
        {
            //出错处理
        }
    }

    public static void ReplyTopic(int uid,DateTime t)//用户回帖
    {
        try
        {
            SaveData.IncMemRepNum(uid, t);
        }
        catch (System.Exception ex)
        {
            //出错处理
        }
    }

    public static void Login(members m,string ip)
    {
        try
        {
            SaveData.UpdateUserLogin(m, ip);
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public static void Exit(int uid,int minutes)//下线处理
    {
        try
        {
            SaveData.IncMemOnlineTime(uid, minutes);
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public void GetOnlineTime()
    {
        this.online = ConvertData.ToTimeSpan(onlineTime);
    } 

    public static bool IsWaterPost(DateTime dt)
    {
        if (dt == null)
            return true;
        TimeSpan ts = DateTime.Now - dt;
        double ses = ts.TotalSeconds;
        if (ses < 8)
            return true;
        else 
            return false;
    }


}